<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	HTML export class
*	Export just a plain HTML file listing of the bibliography (no meta data etc.)
*****/
class HTMLEXPORT
{
// Constructor
	function HTMLEXPORT($db, $vars = FALSE)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/file/FILE.php");
		$this->file = new FILE($this->db);
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/file/export/EXPORTCOMMON.php");
		$this->common = new EXPORTCOMMON($this->db);
// Perform some system admin
		$this->file->tidyFiles();
	}
// Export options
	function display()
	{
		include_once("core/html/FORM.php");
		$this->template->setVar('heading', $this->messages->text("heading", "exportHtml"));
		$this->template->setVar('formStart', FORM::formHeader("exportHtmlProcess"));
		$checked = $this->session->getVar("exportHtml_link") ? 'CHECKED' : FALSE;
		$pString = FORM::checkbox($this->messages->text("misc", "exportHyperlink"), "exportHtml_link", $checked);
		$pString .= MISC::p(FORM::formSubmit("Proceed"));
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// write $this->pString to file.  If file exists, it is written over.
	function process()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "exportHtml"));
		if(array_key_exists('exportHtml_link', $this->vars))
		{
			global $_SERVER;
			$link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?action=resourceView&id=";
			$this->session->setVar('exportHtml_link', TRUE);
		}
		else
		{
			$link = FALSE;
			$this->session->delVar('exportHtml_link');
		}
		$this->htmlHeader();
		$this->getData($link);
		$this->htmlFooter();
		list($fileName, $fullFileName) = $this->file->createFileName($this->pString, '.html');
		if(!$fullFileName)
			$this->failure($this->errors->text("file", "write", ": $fileName"));
		if(!$fp = fopen("$fullFileName", "w"))
			$this->failure($this->errors->text("file", "write", ": $fileName"));
		if(!fputs($fp, $this->pString))
			$this->failure($this->errors->text("file", "write", ": $fileName"));
		fclose($fp);
		$this->common->writeFilenameToSession($fileName);
		$pString = $this->common->listFiles();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Write the HTML header to $this->pString
	function htmlHeader()
	{
		$this->pString = "<head><title></title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n</head><body>\n\n";
	}
// Write the HTML footer to $this->pString
	function htmlFooter()
	{
		$this->pString .= "\n</body></html>";
	}
// list and HTML format all files in this sessionID's folder
	function listFiles()
	{
		include_once("core/html/MISC.php");
		list($dirName, $deletePeriod, $array) = $this->file->listFiles();
		if(!$dirName)
			$this->failure($this->errors->text("file", "read"));
		$pString = MISC::p($this->messages->text("file", "contents"));
		foreach($array AS $key => $value)
			$pString .= MISC::a("link", $key, $dirName . '/' . $key, "_blank") . MISC::br();
		$minutes = $deletePeriod/60;
		$pString .= MISC::hr();
		$pString .= MISC::p($this->messages->text("file", "warning", " $minutes "));
		return $pString;
	}
// get data from database
	function getData($link)
	{
		include_once("core/styles/BIBSTYLE.php");
		$bibStyle = new BIBSTYLE($this->db, "html");
		if(!$sql = $this->session->getVar('sql_stmt'))
			$this->failure($this->errors->text("file", "noSql"));
		$recordset = $this->db->query(stripslashes($sql));
		while($row = $this->db->loopRecordSet($recordset))
		{
			$hyperlink = $link ? "&nbsp;&nbsp;" . MISC::a('link', "[*]", $link . $row['resourceId']) : $link;
			$this->pString .= $bibStyle->process($row) . $hyperlink . "<br />\n";
		}
	}
// Error handling
	function failure($error)
	{
		$this->template->setVar('body', $error);
		$pString = $this->template->process();
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $pString);
	}
}
?>
